import java.util.*;
public class TestDemo {

    // 给一些数据，统计每个数据出现的次数
    public static Map<Integer,Integer> func1(int[] arr){
        Map<Integer,Integer> map = new HashMap<>();
        for (Integer i:arr) {
            if (map.get(i) == null){
                map.put(i,1);
            }else{
                map.put(i,map.get(i)+1);
            }
        }
        return map;
    }
    // 给一些数据，将其去重
    public static Set<Integer> func2(int[] arr){
        Set<Integer> set = new HashSet<>();
        for (Integer i:arr) {
            set.add(i);
        }
        return set;
    }
    // 给一些数据，找出其中第一个重复的数据
    public static int func3(int[] arr){
        Set<Integer> set = new HashSet<>();
        for (Integer i:arr) {
            if (!set.isEmpty() && set.contains(i)){
                return i;
            }
            set.add(i);
        }
        String str = "ddd3e";
        char c = str.charAt(0);
        return -1;
    }

    public int numJewelsInStones(String jewels, String stones) {
        int count = 0;
        Set<Character> set = new HashSet<>();
        for (Character c : jewels.toCharArray()) {
            set.add(c);
        }
        for (Character c : stones.toCharArray()) {
            if(set.contains(c)){
                count++;
            }
        }
        return count;
    }

    //
    public static List<String> topKFrequent(String[] words, int k) {
        // 求单词出现次数
        Map<String,Integer> map = new HashMap<>();
        for (String s:words) {
            int val = map.getOrDefault(s,0);
            map.put(s,val+1);
        }
        // 建立小根堆
        PriorityQueue<Map.Entry<String,Integer>> pq = new PriorityQueue<>(k,new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o1.getValue().compareTo(o2.getValue()) == 0){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue() - o2.getValue();
            }
        });
        //
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String,Integer> entry : entries) {
            if(pq.size() < k){
                pq.offer(entry);
            }else{
                // 判断频率是否相同，如果相同，比较单词的大小，单词小的入堆
                if (pq.peek().getValue().compareTo(entry.getValue()) == 0){
                    if (pq.peek().getKey().compareTo(entry.getKey()) > 0){
                        pq.poll();
                        pq.offer(entry);
                    }
                }else{
                    if (pq.peek().getValue().compareTo(entry.getValue()) < 0){
                        pq.poll();
                        pq.offer(entry);
                    }
                }
            }
        }
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            ret.add(pq.poll().getKey());
        }
        Collections.reverse(ret);
        return ret;
    }

    public static void main(String[] args) {
        String[] strings = {"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"};
        List<String> list = topKFrequent(strings,4);
//        System.out.println(list);
    }


    public static void main2(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        System.out.println(set);

        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public static void main1(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("hello",3);
        map.put("world",2);
        map.put("haha",5);
        map.put("bit",1);
        map.put("bit",4);// key如果相同，value值会被覆盖
        System.out.println(map);
        System.out.println(map.get("bit"));// 没有，则返回 null
        System.out.println(map.getOrDefault("bite",66));// 没有找到则默认打印后面的66的值
        System.out.println(map.remove("haha"));// 打印 5

        // 获取所有的 key值
        Set<String> keys = map.keySet();// set集合存储的元素不重复!!
        System.out.println(keys);

        //
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String,Integer> entry : entries) {
            System.out.println(entry.getKey() + " ---> " + entry.getValue());
        }
    }
}
